<?php

class TradePrice extends DataObject {
    private static $singular_name = "Trade Price";
    private static $plural_name = "Trade Prices";
	
	private static $db = array(
        'Price' => 'TradeCurrency',
        'CompanyUnit' => 'Int',
        'MemberUnit' => 'Int'
    );
	
	private static $has_one = array(
		'TradeSetting' => 'TradeSetting'
	);
	
	private static $searchable_fields = array(
    	'Price' => array(
            'filter' => 'GreaterThanOrEqualFilter'
        ),
		'CompanyUnit' => array(
            'filter' => 'GreaterThanOrEqualFilter'
        ),
        'MemberUnit' => array(
            'filter' => 'GreaterThanOrEqualFilter'
        ),
        'TradeSettingID'
    );

    private static $summary_fields = array(
    	'Price',
    	'CompanyUnit',
        'MemberUnit',
        'TradeSetting.Title'
    );
	
	private static $casting = array(
		'AvailableCompanyUnit' => 'Int',
		'AvailableMemberUnit' => 'Int',
		'LimitMemberUnit' => 'Int',
		'AvailableUnit' => 'Int',
		'AvailableValue' => 'TradeCurrency'
	);
	
	public function fieldLabels($includerelations = true) {
        $labels = parent::fieldLabels($includerelations);

		$labels['Price'] = _t('TradePrice.PIRCE', 'Price');
		$labels['CompanyUnit'] = _t('TradePrice.COMPANY_UNIT', 'Company Unit');
		$labels['MemberUnit'] = _t('TradePrice.MEMBER_UNIT', 'Member Unit');
		$labels['TradeSetting'] = _t('TradePrice.TRADE_SETTING', 'Trade Setting');
		$labels['TradeSettingID'] = _t('TradePrice.TRADE_SETTING', 'Trade Setting');
		$labels['TradeSetting.Title'] = _t('TradePrice.TRADE_SETTING', 'Trade Setting');

        return $labels;
    }
	
	function onAfterWrite(){
		parent::onAfterWrite();
		if($this->isChanged('CompanyUnit') || $this->isChanged('MemberUnit')){
			if(!$this->AvailableCompanyUnit && !$this->AvailableMemberUnit){
				$this->TradeSetting()->CurrentMargin += $this->TradeSetting()->Margin;
				$this->TradeSetting()->Price += $this->TradeSetting()->Markup;
				$this->TradeSetting()->write();

				while(!$this->TradeSetting()->TradePrice($this->TradeSetting()->Price)->AvailableCompanyUnit && !$this->TradeSetting()->TradePrice($this->TradeSetting()->Price)->AvailableMemberUnit){
					$this->TradeSetting()->CurrentMargin += $this->TradeSetting()->Margin;
					$this->TradeSetting()->Price += $this->TradeSetting()->Markup;
					$this->TradeSetting()->write();
				}
			}
		}
	}
	
	function getAvailableCompanyUnit(){
		return $this->TradeSetting()->Margin - $this->CompanyUnit;
	}
	
	function getAvailableMemberUnit(){
		return TradeOrder::get()->filter('TradeSettingID', $this->TradeSettingID)->filter('Type', 'Sell')->filter('Price', $this->Price)->sum('Unit') - $this->MemberUnit;
	}

	function getLimitMemberUnit(){
		return $this->TradeSetting()->Margin - ($this->MemberUnit + TradeOrder::get()->filter('TradeSettingID', $this->TradeSettingID)->filter('Type', 'Sell')->filter('Remaining:GreaterThan', 0)->filter('Price', $this->Price)->filter('Status', 'Pending')->sum('Remaining'));
	}
	
	function getAvailableUnit(){
		return $this->AvailableCompanyUnit + $this->AvailableMemberUnit;
	}
	
	function getAvailableValue(){
		return $this->AvailableUnit - $this->Price;
	}

    function canView($member = false) {
        return true;
    }

    function canEdit($member = false) {
        return false;
    }

    function canDelete($member = false) {
        return false;
    }

    function canCreate($member = false) {
        return false;
    }
}
?>